/* 
 * File:   Response.cpp
 * Author: Denys Knertser
 * 
 * Created on February 14, 2012, 2:16 PM
 */
using namespace std;

#include <iostream>
#include "Response.h"

Response::Response(string strPack, string secret, string clientChallenge) : PacketFormat(strPack) {
    value_size=data[0];
    value=data.substr(1,value_size);
    name=data.substr(value_size+1,string::npos);
    if (!checkServerAuth(clientChallenge)) {
        cout << "The server response does not match! Exiting...\n";
        exit(1);
    }
    calculateResponse(secret);
    name="client";
    code = 2;
    data=string(1,value_size)+value+name;
    length=data.length();
}

int Response::calculateResponse(string secret) {
    string buf=string(1,identifier)+secret+value;
    value=string((const char*)MD5((const unsigned char*)buf.c_str(),buf.length(),NULL),16);
    return 0;
}

int Response::checkServerAuth(string challenge) {
    if (name==challenge)
        return 1;
    else
        return 0;
}
